package com.jgssp.ad.sdk.topon.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.anythink.nativead.api.ATNativePrepareInfo;
import com.anythink.nativead.unitgroup.api.CustomNativeAd;

import java.util.List;

import cn.jiguang.jgssp.ad.data.ADJgNativeAdInfo;
import cn.jiguang.jgssp.ad.data.ADJgNativeFeedAdInfo;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgNativeAdListener;

/**
 * @Description:
 * @Author: maipian
 * @CreateDate: 2021/12/7 5:56 PM
 */
public class NativeFeedAd extends CustomNativeAd implements ADJgNativeAdListener {
    private ADJgNativeFeedAdInfo nativeFeedAdInfo;
    private Context context;

    public NativeFeedAd(Context context, ADJgNativeFeedAdInfo nativeFeedAdInfo) {
        this.nativeFeedAdInfo = nativeFeedAdInfo;
        this.context = context;
    }

    @Override
    public void onRenderFailed(ADJgNativeAdInfo adJgNativeAdInfo, ADJgError adJgError) {
    }

    @Override
    public void onRenderSuccess(ADJgNativeAdInfo adJgNativeAdInfo) {

    }

    @Override
    public void onAdReceive(List<ADJgNativeAdInfo> list) {
    }

    @Override
    public void onAdFailed(ADJgError adJgError) {
    }

    @Override
    public void onAdExpose(ADJgNativeAdInfo adJgNativeAdInfo) {
        notifyAdImpression();
    }

    @Override
    public void onAdClick(ADJgNativeAdInfo adJgNativeAdInfo) {
        notifyAdClicked();
    }

    @Override
    public void onAdClose(ADJgNativeAdInfo adJgNativeAdInfo) {
        notifyAdDislikeClick();
    }

    // Lifecycle Handlers
    @Override
    public void prepare(View view, ATNativePrepareInfo nativePrepareInfo) {
        if (view == null || nativeFeedAdInfo == null) {
            return;
        }
        try {
            List<View> clickViewList = nativePrepareInfo.getClickViewList();
            if (view instanceof ViewGroup) {
                View container = ((ViewGroup) view).getChildAt(0);
                nativeFeedAdInfo.registerViewForInteraction((ViewGroup) container, clickViewList.toArray(new View[clickViewList.size()]));
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public View getAdMediaView(Object... object) {
        if (nativeFeedAdInfo != null && context != null && nativeFeedAdInfo.isVideo()) {
            FrameLayout viewGroup = new FrameLayout(context);
            return nativeFeedAdInfo.getMediaView(viewGroup);
        }
        return null;
    }

    @Override
    public String getTitle() {
        if (nativeFeedAdInfo != null) {
            return nativeFeedAdInfo.getTitle();
        }
        return "";
    }

    @Override
    public String getDescriptionText() {
        if (nativeFeedAdInfo != null) {
            return nativeFeedAdInfo.getDesc();
        }
        return "";
    }

    @Override
    public String getMainImageUrl() {
        if (nativeFeedAdInfo != null) {
            return nativeFeedAdInfo.getImageUrl();
        }
        return "";
    }

    @Override
    public String getIconImageUrl() {
        if (nativeFeedAdInfo != null) {
            return nativeFeedAdInfo.getIconUrl();
        }
        return "";
    }

    @Override
    public String getCallToActionText() {
        if (nativeFeedAdInfo != null) {
            return nativeFeedAdInfo.getCtaText();
        }
        return "";
    }


    @Override
    public void destroy() {
        super.destroy();
        if (nativeFeedAdInfo != null) {
            nativeFeedAdInfo.release();
            nativeFeedAdInfo = null;
        }
    }
}
